package main

import (
	"github.com/gin-gonic/gin"
	"mall-go/controller/admin"
	"mall-go/controller/mall"
	"mall-go/middleware"
)

func MallCollectRoute(r *gin.Engine) *gin.Engine {
	r.Use(middleware.CorsMiddleware(), middleware.RecoverMiddleware())
	//------------------------------------------------前台---------------------------------------------------------
	//用户登录
	r.POST("/api/v1/user/login", mall.Login)
	r.POST("/api/v1/user/logout", mall.Logout)
	r.POST("/api/v1/user/register", mall.Register)
	r.GET("/api/v1/user/info", middleware.MallAuthMiddleware(), mall.GetUserDetail)
	r.PUT("/api/v1/user/info", middleware.MallAuthMiddleware(), mall.UpdateInfo)

	//首页
	index := r.Group("/api/v1/index-infos")
	indexController := mall.NewMallIndexController()
	index.GET("", indexController.IndexInfo)

	//地址
	address := r.Group("/api/v1/address")
	address.Use(middleware.MallAuthMiddleware())
	addressController := mall.NewUserAddressController()
	address.GET("", addressController.AddressList)
	address.POST("", addressController.Create)
	address.PUT("", addressController.Update)
	address.GET("/default", addressController.GetDefaultMallUserAddress)
	address.GET("/user/:addressId", addressController.GetMallUserAddress)
	address.DELETE("/:addressId", addressController.Delete)

	//商品&商品分类
	goods := r.Group("/api/v1/goods")
	goods.Use(middleware.MallAuthMiddleware())
	goodsController := mall.NewMallGoodsController()
	goods.GET("/detail/:goodsId", goodsController.GoodsDetail)
	goods.GET("/search", goodsController.Search)
	goods.GET("/categories", goodsController.GetCategories)

	//购物车
	shopCart := r.Group("/api/v1/shop-cart")
	shopCart.Use(middleware.MallAuthMiddleware())
	cartController := mall.NewShopCartController()
	shopCart.GET("/page", cartController.CartItemPageList)
	shopCart.GET("", cartController.CartItemList)
	shopCart.POST("", cartController.SaveNewBeeMallShoppingCartItem)
	shopCart.PUT("", cartController.UpdateNewBeeMallShoppingCartItem)
	shopCart.DELETE("/:newBeeMallShoppingCartItemId", cartController.DeleteNewBeeMallShoppingCartItem)
	shopCart.GET("/new/settle", cartController.ToSettle)

	//订单
	order := r.Group("/api/v1/order")
	order.Use(middleware.MallAuthMiddleware())
	orderController := mall.NewMallOrderController()
	order.POST("/saveOrder", orderController.SaveOrder)
	order.GET("/detail/:orderNo", orderController.OrderDetailPage)
	order.GET("", orderController.OrderList)
	order.PUT("/:orderNo/cancel", orderController.CancelOrder)
	order.PUT("/:orderNo/finish", orderController.FinishOrder)
	order.GET("/paySuccess", orderController.PaySuccess)

	//------------------------------------------------后台---------------------------------------------------------
	//管理员
	adminUser := r.Group("/manage-api/v1/adminUser")
	manageUserController := admin.NewAdminManageUserController()
	adminUser.POST("/login", manageUserController.Login)
	adminUser.GET("/profile", middleware.AdminAuthMiddleware(), manageUserController.Profile)
	adminUser.PUT("/password", middleware.AdminAuthMiddleware(), manageUserController.UpdatePassword)
	adminUser.PUT("/name", middleware.AdminAuthMiddleware(), manageUserController.UpdateName)
	adminUser.DELETE("/logout", manageUserController.Logout)

	//注册用户模块
	registerUser := r.Group("/manage-api/v1/users")
	registerUserController := admin.NewRegisterUserController()
	registerUser.Use(middleware.AdminAuthMiddleware())
	registerUser.GET("", registerUserController.List)
	registerUser.PUT("/:lockStatus", registerUserController.LockUser)

	//轮播图
	carousels := r.Group("/manage-api/v1/carousels")
	carouselsController := admin.NewCarouselsController()
	carousels.Use(middleware.AdminAuthMiddleware())
	carousels.GET("", carouselsController.List)
	carousels.POST("", carouselsController.Save)
	carousels.PUT("", carouselsController.Update)
	carousels.GET("/:id", carouselsController.Info)
	carousels.DELETE("", carouselsController.BatchDelete)

	//商品分类
	categories := r.Group("/manage-api/v1/categories")
	categoryController := admin.NewCategoryController()
	categories.Use(middleware.AdminAuthMiddleware())
	categories.GET("", categoryController.List)
	//categories.GET("/select", categoryController.ListForSelect)
	categories.POST("", categoryController.Save)
	categories.PUT("", categoryController.Update)
	categories.GET("/:id", categoryController.Info)
	categories.DELETE("", categoryController.BatchDelete)

	//商品
	adminGoods := r.Group("/manage-api/v1/goods")
	adminGoods.Use(middleware.AdminAuthMiddleware())
	newGoodsController := admin.NewGoodsController()
	adminGoods.GET("/list", newGoodsController.List)
	adminGoods.POST("", newGoodsController.Save)
	adminGoods.PUT("", newGoodsController.Update)
	adminGoods.GET("/info/:id", newGoodsController.Info)
	adminGoods.PUT("/status/:sellStatus", newGoodsController.BatchEdit)

	//首页配置
	adminIndex := r.Group("/manage-api/v1/indexConfigs")
	adminIndex.Use(middleware.AdminAuthMiddleware())
	newIndexController := admin.NewIndexController()
	adminIndex.GET("", newIndexController.List)
	adminIndex.POST("", newIndexController.Save)
	adminIndex.PUT("", newIndexController.Edit)
	adminIndex.GET("/:id", newIndexController.Info)
	adminIndex.DELETE("", newIndexController.BatchDelete)

	//订单
	adminOrder := r.Group("/manage-api/v1/orders")
	adminOrder.Use(middleware.AdminAuthMiddleware())
	adminOrderController := admin.NewOrderController()
	adminOrder.GET("", adminOrderController.List)
	adminOrder.GET("/detail/:orderId", adminOrderController.OrderDetailPage)
	adminOrder.PUT("/checkDone", adminOrderController.CheckDone)
	adminOrder.PUT("/checkOut", adminOrderController.CheckOut)
	adminOrder.PUT("/close", adminOrderController.CloseOrder)

	//图片上传 todo 这部分等我测试完后把项目搭到服务器再做，现在就先用着别人的接口
	//默认申请的内存大小为32M，这里可以设置小一点，限制上传文件的大小 todo 未测试是否可用,不清楚是否会影响到上面的路由
	//r.MaxMultipartMemory = 8 << 20 // 8M
	//upload := r.Group("/manage-api/v1/upload")
	//uploadController := admin.NewUploadController()
	//upload.POST("/file", uploadController.Upload)
	//upload.POST("/files", uploadController.MultiUpload)
	return r
}
